package com.x.erp.dao;

import com.x.erp.model.MultilevelActivityShare;
import com.x.framework.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * 多级营销活动分享记录信息DAO
 */
@Repository
public class MultilevelActivityShareDao extends BaseDao {

	private final static String selectMultilevelActivityShareListSql = "SELECT " +
			"emas.*, " +
			"ema.ACTIVITY_LEVEL, " +
			"ema.ACTIVITY_BOUNTY, " +
			"ema.ACTIVITY_GIFT, " +
			"ema.ACTIVITY_DURATION, " +
			"ema.ACTIVITY_PUBLISH_TIME, " +
			"ema.ACTIVITY_END_TIME, " +
			"ema.ACTIVITY_STATE, " +
			"ec.CAR_TITLE, " +
			"ec.CAR_IMAGE_URL, " +
			"ec.CAR_RETAIL_PRICE, " +
			"ec.CAR_GUIDE_PRICE, " +
			"ec.CAR_MILEAGE, " +
			"ec.CAR_AUTH_STATE, " +
			"ec.CAR_LICENCE_DATE, " +
			"ec.CAR_LICENCE_STATE, " +
			"cu.USER_NICKNAME, " +
			"cu.USER_MOBILE, " +
			"cu.USER_HEAD " +
			"FROM " +
			"erp_multilevel_activity_share emas, " +
			"erp_multilevel_activity ema, " +
			"erp_car ec, " +
			"x_client.client_user cu " +
			"WHERE " +
			"ema.ACTIVITY_ID = emas.ACTIVITY_ID " +
			"AND ema.CAR_ID = ec.CAR_ID " +
			"AND emas.USER_ID = cu.USER_ID ";

	/**
	 * 查询多级营销活动分享记录列表
	 * @param multilevelActivityShare
	 * @return
	 * @throws Exception
	 */
	public List<MultilevelActivityShare> selectMultilevelActivityShareList(MultilevelActivityShare multilevelActivityShare) throws Exception{
		StringBuilder sql = new StringBuilder(selectMultilevelActivityShareListSql);
		List<Object> params = new ArrayList<>();
		searchCondition(sql, params, multilevelActivityShare);
		return this.queryForList(sql.toString(), MultilevelActivityShare.class, params, multilevelActivityShare);
	}

	private final static String selectMultilevelActivityShareNumSql = "SELECT COUNT(emas.SHARE_ID) FROM erp_multilevel_activity_share emas WHERE 1=1 ";
	/**
	 * 查询多级营销活动分享记录数量
	 * @param multilevelActivityShare
	 * @return
	 * @throws Exception
	 */
	public int selectMultilevelActivityShareNum(MultilevelActivityShare multilevelActivityShare) throws Exception{
		StringBuilder sql = new StringBuilder(selectMultilevelActivityShareNumSql);
		List<Object> params = new ArrayList<>();
		searchCondition(sql, params, multilevelActivityShare);
		return this.queryForInt(sql.toString(), params);
	}

	/**
	 * 查询条件
	 * @param sql
	 * @param params
	 * @param multilevelActivityShare
	 */
	private void searchCondition(StringBuilder sql, List<Object> params, MultilevelActivityShare multilevelActivityShare) {
		if(multilevelActivityShare != null) {
			if(StringUtils.isNotEmpty(multilevelActivityShare.getShareId())){
				sql.append("AND emas.SHARE_ID = ? ");
				params.add(multilevelActivityShare.getShareId());
			}
			if(StringUtils.isNotEmpty(multilevelActivityShare.getShareParentId())){
				sql.append("AND emas.SHOP_PARENT_ID = ? ");
				params.add(multilevelActivityShare.getShareParentId());
			}
			if(StringUtils.isNotEmpty(multilevelActivityShare.getUserId())){
				sql.append("AND emas.USER_ID = ? ");
				params.add(multilevelActivityShare.getUserId());
			}
			if(multilevelActivityShare.getActivityId() != null){
				sql.append("AND emas.ACTIVITY_ID = ? ");
				params.add(multilevelActivityShare.getActivityId());
			}
			if(multilevelActivityShare.getShareState() != null){
				sql.append("AND emas.SHARE_STATE = ? ");
				params.add(multilevelActivityShare.getShareState());
			}
		}
	}
}
